package com.jhj.wx.utils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;

import redis.clients.jedis.Jedis;

import com.jhj.utils.HttpClientUtil;
import com.jhj.utils.StringUtil;
import com.jhj.utils.redis.RedisUtil;

/**
 * 微信支付配置
 *
 */
public class WxUtil {

	// 公众号id
	public static String appId = "wx1da3f16a433d8bd8";

	// 商户id
	public static String mchId = "1259309301";

	// 密钥
	public static String appSecret = "46a8b0480da4a2338072338478a84fb5";

	// 商户KEY
	public static String wxKey = "beijingjiahejiadingdangdaojiafuw";

	// 统一支付接口
	public static String payUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

	// 微信get_code_url
	public static String getCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize";

	// 微信get_openid_url
	public static String getOpenIdUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";

	// 微信get_refresh_token_url
	public static String getRefreshTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token";

	// 微信get_check_token_url
	public static String getCheckTokenUrl = "https://api.weixin.qq.com/sns/auth";

	/**
	 * 获取access_token 访问
	 * wx_get_openid_url=https://api.weixin.qq.com/sns/oauth2/access_token
	 * 
	 * @return
	 */
	public static Map<String, Object> getAccessToken(String code) {
		String result = "";
		try {

			String cacheAccessTokenStr = RedisUtil.getInstance().strings().get("wx_access_token");
			if (!StringUtil.isEmpty(cacheAccessTokenStr)) {
				return JsonUtil.stringToMap(cacheAccessTokenStr);
			}

			Map<String, String> map = new HashMap<String, String>();
			map.put("appid", WxUtil.appId);
			map.put("secret", WxUtil.appSecret);
			map.put("code", code);
			map.put("grant_type", "authorization_code");
			String openIdUrl = WxUtil.getOpenIdUrl;
			System.out.println(openIdUrl);
			System.out.println("");
			result = HttpClientUtil.get(openIdUrl, map);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JsonUtil.stringToMap(result);
	}

	/**
	 * 刷新access_token（如果需要）
	 * wx_get_refresh_token_url=https://api.weixin.qq.com/sns/oauth2/refresh_token
	 * 
	 * @return
	 */
	public static Map<String, Object> getRefreshToken(String refreshToken) {
		String result = "";
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("appid", WxUtil.appId);
			map.put("refresh_token", refreshToken);
			map.put("grant_type", "refresh_token");
			String refreshUrl = WxUtil.getRefreshTokenUrl;
			result = HttpClientUtil.get(refreshUrl, map);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return JsonUtil.stringToMap(result);
	}

	/**
	 * 检验授权凭证（access_token）是否有效
	 * wx_check_token_url=https://api.weixin.qq.com/sns/auth
	 * 
	 * @param args
	 */
	public static boolean checkAccessToken(String access_token, String openid) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("access_token", access_token);
		map.put("openid", openid);
		String result = HttpClientUtil.get(WxUtil.getCheckTokenUrl, map);
		Map<String, Object> m = JsonUtil.stringToMap(result);
		boolean flog = m.get("errmsg").equals("ok");
		return flog;
	}

	public static String getNonceStr() {
		Random random = new Random();
		return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)), "GBK");
	}

	public static String getAccessToken(String appid, String appsecret) {

		// Map<String,String> map = new HashMap<String,String>();
		String redisKey = "access_token_"+ appid;
		String accessToken = RedisUtil.getInstance().strings().get(redisKey);
		if (accessToken != null && !"".equals(accessToken)) {
			return accessToken;
		}

//		String APP_ID = "wx1da3f16a433d8bd8";// 微信id
//		String APP_SECRET = "46a8b0480da4a2338072338478a84fb5";// 微信秘钥
		// 微信令牌请求网址(由微信提供)
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret="
				+ appsecret;

		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求
			http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
			System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
			http.connect();
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message = new String(jsonBytes, "UTF-8");
			JSONObject demoJson = new JSONObject(message);
			accessToken = demoJson.getString("access_token");
			// expiresIn = demoJson.getString("expires_in");

			// map.put("access_token", accessToken);
			// map.put("expires_in", expiresIn);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(accessToken);

		RedisUtil.getInstance().strings().set(redisKey, accessToken);
		RedisUtil.getInstance().keys().expire(redisKey, 1800);
		return accessToken;
	}

	public static String getTicket(String access_token) {

		String ticket = RedisUtil.getInstance().strings().get("ticket");
		if (ticket != null && !"".equals(ticket)) {
			return ticket;
		}
		// 获取票据的网址(由微信提供)
		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";

		try {
			URL urlGet = new URL(url);
			HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
			http.setRequestMethod("GET"); // 必须是get方式请求
			http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
			System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
			http.connect();
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] jsonBytes = new byte[size];
			is.read(jsonBytes);
			String message = new String(jsonBytes, "UTF-8");
			JSONObject demoJson = new JSONObject(message);
			ticket = demoJson.getString("ticket");
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		RedisUtil.getInstance().strings().set("ticket", ticket);
		RedisUtil.getInstance().keys().expire("ticket", 1800);
		return ticket;
	}

}
